package cn.ruanyun.backInterface.modules.business.sensitiveWords.controller;

import cn.ruanyun.backInterface.common.enums.SensitivityTypeEnum;
import cn.ruanyun.backInterface.common.utils.PageUtil;
import cn.ruanyun.backInterface.common.utils.ResultUtil;
import cn.ruanyun.backInterface.common.vo.PageVo;
import cn.ruanyun.backInterface.common.vo.Result;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.pojo.SensitiveWord;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.service.ISensitiveWordsService;
import cn.ruanyun.backInterface.modules.business.sensitiveWords.utils.SensitiveWordEngine;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * @author tqr
 */
@Slf4j
@RestController
@RequestMapping("/ruanyun/sensitiveWords")
@Transactional
public class SensitiveWordsController {

    @Autowired
    private ISensitiveWordsService iSensitiveWordsService;

    @RequestMapping(value = "/get", method = RequestMethod.POST)
    @ApiOperation(value = "通过id获取")
    public Result<SensitiveWord> get( String id) {

        SensitiveWord sensitiveWords = iSensitiveWordsService.getById(id);
        return new ResultUtil<SensitiveWord>().setData(sensitiveWords);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.POST)
    @ApiOperation(value = "获取全部数据")
    public Result<List<SensitiveWord>> getAll() {

        List<SensitiveWord> list = iSensitiveWordsService.list();
        return new ResultUtil<List<SensitiveWord>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.POST)
    @ApiOperation(value = "分页获取")
    public Result<IPage<SensitiveWord>> getByPage(PageVo page) {

        IPage<SensitiveWord> data = iSensitiveWordsService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<SensitiveWord>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<SensitiveWord> saveOrUpdate(SensitiveWord sensitiveWords) {

        if (iSensitiveWordsService.saveOrUpdate(sensitiveWords)) {
            return new ResultUtil<SensitiveWord>().setData(sensitiveWords);
        }
        return new ResultUtil<SensitiveWord>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "批量通过id删除")
        public Result<Object> delAllByIds( String[] ids) {

        for (String id : ids) {
            iSensitiveWordsService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }


    /**
     * 敏感词过滤
     *
     * @param text
     * @return
     */
    @PostMapping(value = "/word/filter")
    @ResponseBody
    @ApiOperation(value = "其他敏感词过滤")
    public Result<Object> sensitiveWordFiltering(String text) {

        Set<String> set = iSensitiveWordsService.sensitiveWordFiltering(text,SensitivityTypeEnum.ALL);

        if (set.size() > 0){
            return new ResultUtil<>().setData(set,"包含敏感词",208);
        }
        return new ResultUtil<>().setSuccessMsg("不包含敏感词");
    }

    /**
     * 活动敏感词过滤
     *
     * @param text
     * @return
     */
    @PostMapping(value = "/activity/filter")
    @ResponseBody
    @ApiOperation(value = "广告敏感词过滤")
    public Result<Object> sensitiveActivityFiltering(String text) {

        Set<String> set = iSensitiveWordsService.sensitiveWordFiltering(text,null);

        if (set.size() > 0){
            return new ResultUtil<>().setData(set,"包含敏感词",208);
        }
        return new ResultUtil<>().setSuccessMsg("不包含敏感词");
    }

}
